package top.rainbowecho.gateway.filter;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Component;
import top.rainbowecho.common.util.CommonKey;

import javax.servlet.http.HttpServletRequest;

/**
 * 处理敏感头被过滤
 *
 * @author rainbow
 * @since 2020/1/25 12:26
 */
@Component
public class HeaderDeliveryFilter extends ZuulFilter {
    @Override
    public String filterType() {
        return FilterConstants.PRE_TYPE;
    }

    @Override
    public int filterOrder() {
        return 0;
    }

    @Override
    public boolean shouldFilter() {
        return true;
    }

    @Override
    public Object run() throws ZuulException {
        // 将token 放到自己定义的请求头中
        RequestContext currentContext = RequestContext.getCurrentContext();
        HttpServletRequest request = currentContext.getRequest();
        String jwtToken = request.getHeader(HttpHeaders.AUTHORIZATION);
        currentContext.addZuulRequestHeader(CommonKey.OWN_AUTHORIZATION, jwtToken);
        return null;
    }
}
